

//
//  MyCollectionHeaderView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/31.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyCollectionHeaderView.h"
@interface MyCollectionHeaderView()
@property(nonatomic,strong) NSString *headImg;
@property(nonatomic,strong) NSString *nickNamed;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@end
@implementation MyCollectionHeaderView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        self.autoLogIn = NO;
//        self.headImg = [UserDefaultsHelper readHeadimg];
        //        self.nickNamed = [UserDefaultsHelper readNickName];
        [self setUpUI];
        self.manager = [AFHTTPSessionManager manager];
        self.backgroundColor = kOrangeBg;
        self.certificationL.layer.masksToBounds = YES;
        self.certificationL.layer.cornerRadius = 2;
        self.certificationL.layer.borderColor  = [UIColor colorWithRed:170/255.0 green:203/255.0 blue:123/255.0 alpha:1].CGColor;
        self.certificationL.layer.borderWidth  = 1;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(simplecertification:) name:@"simplecertification" object:nil];
    }
    return self;
}
- (void)setReloadDate:(BOOL)reloadDate
{
    _reloadDate = reloadDate;
    self.autoLogIn = [UserDefaultsHelper readAutoLogIn];
    [self reloadInternetData];
    XLog(@"self.autoLogIn:%@",self.autoLogIn);
    NSInteger empty = [EmptyModel stringIsNullOrEmpty:self.autoLogIn];
    XLog(@"[UserDefaultsHelper readType]:%@",[UserDefaultsHelper readType]);
    if ([self.autoLogIn isEqualToString:@"NO"]||empty ==1) {
        self.autoLogInBtn.hidden = NO;
        self.nameL.text = @"未登录";
        self.nameL.textColor = kLabel102Color;
        self.markL.hidden = YES;
        self.certificationL.text = @"未认证";
        self.markL.text = @"用户未认证 | 暂无公司职务信息";
        self.headIV.image = [UIImage imageNamed:@"headImg"];
    }else {
        self.autoLogInBtn.hidden = YES;
        self.markL.hidden = NO;
        if ([[UserDefaultsHelper readType]integerValue] <= 0) {
            self.certificationL.text = @"未认证";
        }else if ([[UserDefaultsHelper readType]integerValue] == 1) {
            self.certificationL.text = @"个人认证";
        }else {
            self.certificationL.text = @"企业认证";
        }
        XLog(@"[UserDefaultsHelper readNickName]:%@",[UserDefaultsHelper readNickName]);
        if ([UserDefaultsHelper readNickName].length) {
            self.nameL.text = [UserDefaultsHelper readNickName];
        }else {
            self.nameL.text = @"暂未设置昵称";
            
        }
        [self.headIV sd_setImageWithURL:[NSURL URLWithString:[UserDefaultsHelper readHeadimg]] placeholderImage:[UIImage imageNamed:@"headImg"]];
    }
}
#pragma mark-发送---实名认证
-(void)reloadInternetData
{
//    self.urlString = [NSString stringWithFormat:@"%@/simplecertification/Authenticationstatus",kUrl];
//    self.dict = @{
//                  @"uid":[UserDefaultsHelper readUid]
//                  };
//    self.urlString = [NSString stringWithFormat:@"%@/simplecertification/Authenticationstatus",kUrl];
//    self.dict = @{
//                  @"uid":[UserDefaultsHelper readUid]
//                  };
//    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSString *message = [responseObject objectForKey:@"message"];
//        NSInteger date = [[responseObject objectForKey:@"date"]integerValue];
//        XLog(@"responseObject:%@",responseObject);
//        if (date == 5) {
//            [UserDefaultsHelper saveType:@"" value:@"1"];
//        }else  if (date == 2) {
//            [UserDefaultsHelper saveType:@"" value:@"2"];
//        }
//
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//    }];
    //使用GCD的信号量 dispatch_semaphore_t 创建同步请求
    dispatch_group_t group =dispatch_group_create();
    dispatch_queue_t globalQueue=dispatch_get_global_queue(0, 0);
    dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
    dispatch_group_async(group, globalQueue, ^{
        for (int i = 0; i<3; i++) {
            if (i == 0) {
                self.urlString = [NSString stringWithFormat:@"%@/simplecertification/Authenticationstatus",kUrl];
                self.dict = @{
                              @"uid":[UserDefaultsHelper readUid]
                              };
            }else if (i == 1) {
                self.urlString = [NSString stringWithFormat:@"%@/user/selectUserCertion",kUrl];
                self.dict = @{
                              @"uid":[UserDefaultsHelper readUid],
                              @"token":[UserDefaultsHelper readToken]
                              };
            }else {
                self.dict = @{
                              @"userId":[UserDefaultsHelper readUid],
                              @"token":[UserDefaultsHelper readToken]
                              };
                XLog(@"self.dict:%@",self.dict);
                self.urlString = [NSString stringWithFormat:@"%@/user/checkToken",kUrl];
            }
            [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                XLog(@"MyShopHeadView:%@",responseObject);
                if (i == 0) {
                    NSNumber *date = [responseObject objectForKey:@"date"];
                    XLog(@"responseObject:%@",responseObject);
                    if ([date compare:[NSNumber numberWithInt:5]] == NSOrderedSame) {
                        [UserDefaultsHelper saveType:@"" value:@"1"];
                    }else if ([date compare:[NSNumber numberWithInt:2]] == NSOrderedSame) {
                        [UserDefaultsHelper saveType:@"" value:@"2"];
                    }
                }else if (i == 1) {
                     NSDictionary *date = [responseObject objectForKey:@"date"];
                    XLog(@"selectUserCertiondate:%@",responseObject);
//                     NSNumber *date = [responseObject objectForKey:@"date"];
       
                    if([[date allKeys] containsObject:@"companyname"]) {
                        NSString *companyname = [date objectForKey:@"companyname"];
                        NSString *employeepositions = [date objectForKey:@"employeepositions"];
//                        if (companyname.length) {
                            self.markL.text = [NSString stringWithFormat:@"%@ | %@",companyname,employeepositions];
//                        }
                    }else {
                        self.markL.text = @"未进行企业认 | 暂无公司职务信息";
//                        self.certificationL.text = @"未认证";
                    }
                }else {
                    NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
                    if (status != 200) {
//                        [LogOutAccount presentOutAccount];
//                        XLog(@"showWarningAccount");
//                        [UIAlertViewWarningAccount showWarningAccount];
                    }
                }
                dispatch_semaphore_signal(semaphore);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                XLog(@"error:%@",error);
                dispatch_semaphore_signal(semaphore);
            }];
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        }
        
    });
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        XLog(@"%@---全部结束。。。",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    });
}

-(void)setUpUI
{
    self.settingBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth*0.9, kHeight*0.15, 24, 24)];
    [self.settingBtn setImage:[UIImage imageNamed:@"icon_131"] forState:UIControlStateNormal];
    [self.settingBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.settingBtn];
    self.settingBtn.tag = 0;
    self.bgIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, kHeight/2, kWidth, kHeight/2)];
    [self addSubview:self.bgIV];
//    self.bgIV.backgroundColor = [UIColor blueColor];
    self.bgIV.image = [UIImage imageNamed:@"bg_share_start"];
    self.headIV = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth-80)/2, (kHeight-80)/2, 80, 80)];
    [self addSubview:self.headIV];
    self.headIV.layer.masksToBounds = YES;
    self.headIV.layer.cornerRadius = 40;
    self.headIV.layer.borderWidth = 2;
    self.headIV.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.headIV.backgroundColor = [UIColor cyanColor];
//    [self.headIV sd_setImageWithURL:[NSURL URLWithString:self.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    self.nameL = [[UILabel  alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headIV.frame)+8, kUIScreen.size.width, 23)];
    self.nameL.text = [UserDefaultsHelper readNickName];
    self.nameL.font = [UIFont systemFontOfSize:14];
    self.nameL.textColor = kLabel153Color;
    self.nameL.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameL];
    self.certificationL = [[UILabel  alloc]initWithFrame:CGRectMake((kWidth -60)/2, CGRectGetMaxY(self.nameL.frame)+4, 60, 23)];
    self.certificationL.text = @"certificationL";
    self.certificationL.font = [UIFont systemFontOfSize:12];
    self.certificationL.textColor = [UIColor colorWithRed:153/255.0 green:201/255.0 blue:103/255.0 alpha:1];
    self.certificationL.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.certificationL];
    self.autoLogInBtn = [[UIButton alloc]initWithFrame:CGRectMake((kUIScreen.size.width-80)/2, CGRectGetMaxY(self.nameL.frame)+4, 80, 30)];
    [self addSubview:self.autoLogInBtn];
    [self.autoLogInBtn setTitle:@"登录" forState:UIControlStateNormal];
    self.autoLogInBtn.layer.cornerRadius = 3;
    self.autoLogInBtn.tag = 1;
    [self.autoLogInBtn addTarget: self action:@selector(clickAutoLogInBtn:) forControlEvents:UIControlEventTouchUpInside];
//    self.autoLogInBtn.layer.borderColor  = kOrangeBg.CGColor;
    self.autoLogInBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.autoLogInBtn.backgroundColor = kOrangeBg;
    if (!self.autoLogIn) {
        self.autoLogInBtn.hidden = NO;
    }else {
        self.autoLogInBtn.hidden = YES;
    }
    self.markL = [[UILabel  alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.certificationL.frame)+9, kUIScreen.size.width, 17)];
    self.markL.text = @"";
    self.markL.font = [UIFont systemFontOfSize:12];
    self.markL.textColor = kLabel102Color;
    self.markL.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.markL];
    
}
-(void)clickBtn:(UIButton *)clickBtn
{
    if ([self.delegate respondsToSelector:@selector(myCollectionHeaderView:withTag:)]) {
        [self.delegate myCollectionHeaderView:self withTag:clickBtn.tag];
    }
}
-(void)clickAutoLogInBtn:(UIButton *)clickAutoLogInBtn
{
    if ([self.delegate respondsToSelector:@selector(myCollectionHeaderView:withTag:)]) {
        [self.delegate myCollectionHeaderView:self withTag:clickAutoLogInBtn.tag];
    }
}
-(void)reloadInternet
{
////    [self reloadData];
//    [self layoutIfNeeded];
}
-(void)simplecertification:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WSProgressHUD dismiss];
    });
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
